package com.jushu.storbox.view.resolve;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.jushu.storbox.R;
import com.jushu.storbox.actions.FolderAction;
import com.jushu.storbox.adapter.TargetSearchDisplayAdapter;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.constant.UiConstant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileType;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.newfragment.argument.FileActionArgs;
import com.jushu.storbox.operation.group.TargetSearchOperation;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.vo.PagedList;

public class TargetSearchDisplayResolve
        extends ListDisplayResolve<FileFolderInfo, TargetSearchOperation<FileFolderInfo>, TargetSearchDisplayAdapter> {
    private final static String TAG = TargetSearchDisplayResolve.class.getSimpleName();

    private List<FileFolderInfo> datas = new ArrayList<FileFolderInfo>();
    private FileActionArgs folderArgument = new FileActionArgs();
    Stack<FileActionArgs> history = new Stack<FileActionArgs>();
    private String ownerId;

    public TargetSearchDisplayResolve(Context context, String ownerId, String name) {
        super(context);
        folderArgument.setFolderId("0");
        if (!"".equals(name)) {
            folderArgument.setFolderName(name);
        } else {
            folderArgument.setFolderName(context.getResources().getString(R.string.my_cloud_drive));
        }
        folderArgument.setPageIndex(0);
        folderArgument.setTotal(0);
        this.ownerId = ownerId;
    }

    public TargetSearchDisplayResolve(Context context, String ownerId) {
        super(context);
        folderArgument.setFolderId("0");
        folderArgument.setFolderName(context.getResources().getString(R.string.my_cloud_drive));
        folderArgument.setPageIndex(0);
        folderArgument.setTotal(0);
        this.ownerId = ownerId;
    }

    protected TargetSearchDisplayAdapter initTemplateAdapter() {
        return new TargetSearchDisplayAdapter(context, datas);
    }

    protected TargetSearchOperation<FileFolderInfo> initAdapterOperation() {
        return operation;
    }

    @Override
    protected void clickItem(View clickView, int position, long id,
                             FileFolderInfo info) {
        if (FileType.Folder.value() == info.getIsFile()) {
            openFolder(position, info);
        } else {
            LogUtil.e(TAG, "node type not correct!");
        }
    }

    @Override
    protected void refreshDataList() {
        new FolderAction().getSubFolderList(context, UiConstant.FOLDER_ACTION_REFRESH_FOLDER_LIST,
                ownerId, folderArgument.getFolderId(), Constant.FIRST_PAGE_INDEX, msgHandler);
    }

    @Override
    protected void loadDataList() {
        new FolderAction().getSubFolderList(context, UiConstant.FOLDER_ACTION_OPEN_FOLDER,
                ownerId, folderArgument.getFolderId(), Constant.FIRST_PAGE_INDEX, msgHandler);
    }

    @Override
    protected void getMoreDataList() {
        new FolderAction().getSubFolderList(context, UiConstant.FOLDER_ACTION_GET_MORE_FOLDER,
                ownerId, folderArgument.getFolderId(), folderArgument.getPageIndex(), msgHandler);
    }

    public Stack<FileActionArgs> getHistoryStack() {
        return history;
    }

    protected void openFolder(int index, FileFolderInfo info) {
        new FolderAction().getSubFolderList(context, UiConstant.FOLDER_ACTION_OPEN_FOLDER,
                ownerId, info.getId(), Constant.FIRST_PAGE_INDEX, msgHandler);
    }

    public void openParentFolder() {
        if (!history.isEmpty()) {
            history.pop();
        }
        if (!history.isEmpty()) {
            FileActionArgs parentArgument = history.peek();
            new FolderAction().getSubFolderList(context, UiConstant.FOLDER_ACTION_OPEN_PARENT,
                    ownerId, parentArgument.getFolderId(), Constant.FIRST_PAGE_INDEX, msgHandler);
        } else {
            LogUtil.i(TAG, "here no more parent!");
        }
    }

    protected void onItemOpend(String folderId, String folderName) {
        LogUtil.d(TAG, "open: " + folderId);
    }

    ClientExceptionRelateHandler msgHandler = new ClientExceptionRelateHandler() {

        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UiConstant.FOLDER_ACTION_OPEN_FOLDER:
                    onRefreshComplete();
                    PagedList<FileFolderInfo> newPagedList = (PagedList<FileFolderInfo>) msg.obj;
                    folderArgument = new FileActionArgs();
                    folderArgument.setFolderId(newPagedList.getPagedFolderId());
                    folderArgument.setFolderName(newPagedList.getPagedFolderName());
                    folderArgument.setPageIndex(newPagedList.getPageNumber());
//				if(!Constant.ROOT_FOLDER_ID.equals(newPagedList.getPagedFolderId())){
                    history.push(folderArgument);
//				}
                    adapter.resetDatas(newPagedList.getPagedList());
                    folderArgument.incPageIndex();
                    onItemOpend(folderArgument.getFolderId(), folderArgument.getFolderName());
                    break;
                case UiConstant.FOLDER_ACTION_REFRESH_FOLDER_LIST:
                    onRefreshComplete();
                    PagedList<FileFolderInfo> refreshPagedList = (PagedList<FileFolderInfo>) msg.obj;
                    folderArgument.setFolderId(refreshPagedList.getPagedFolderId());
                    folderArgument.setFolderName(refreshPagedList.getPagedFolderName());
                    folderArgument.setPageIndex(refreshPagedList.getPageNumber());
                    folderArgument.incPageIndex();
                    adapter.resetDatas(refreshPagedList.getPagedList());
                    break;
                case UiConstant.FOLDER_ACTION_GET_MORE_FOLDER:
                    onRefreshComplete();
                    PagedList<FileFolderInfo> morePagedList = (PagedList<FileFolderInfo>) msg.obj;
                    if (morePagedList.getPageNumber() == Constant.FIRST_PAGE_INDEX) {
                        adapter.cleanListDatasOnly();
                    }
                    List<FileFolderInfo> tempDatas = morePagedList.getPagedList();
                    adapter.addDatas(tempDatas);
                    if (!tempDatas.isEmpty()) {
                        folderArgument.incPageIndex();
                    }
                    break;
                case UiConstant.FOLDER_ACTION_OPEN_PARENT:
                    onRefreshComplete();
                    PagedList<FileFolderInfo> pagedList = (PagedList<FileFolderInfo>) msg.obj;

                    folderArgument.setFolderId(pagedList.getPagedFolderId());
                    folderArgument.setFolderName(pagedList.getPagedFolderName());
                    folderArgument.setPageIndex(pagedList.getPageNumber());
                    adapter.resetDatas(pagedList.getPagedList());
                    folderArgument.incPageIndex();
                    onItemOpend(folderArgument.getFolderId(), folderArgument.getFolderName());
                    break;
                default:
                    super.handleMessage(msg);
            }
        }

        @Override
        protected void onHandlerBefore(Message msg, String errorCode) {
            onRefreshComplete();
            super.onHandlerBefore(msg, errorCode);
        }

        @Override
        public Context getContext() {
            return context;
        }
    };

    TargetSearchOperation<FileFolderInfo> operation = new TargetSearchOperation<FileFolderInfo>() {
    };
}
